package com.hccl.service.classifier;
import org.tensorflow.Session;
import org.tensorflow.Tensor;

import java.util.*;

/**
 * Created by yang on 2018/8/3.
 */
public abstract class runSession {
    public List<ClassifierResult> getFetch(Session.Runner runner){
        List<ClassifierResult> results = new ArrayList<>();

        List<Tensor<?>> run = runner.fetch("output/action_predictions:0").fetch("output/target_predictions:0").run();
//        Tensor<Long> type_pred = run.get(0).expect(Long.class);
        Tensor<Long> action_pred = run.get(0).expect(Long.class);
        Tensor<Long> target_pred = run.get(1).expect(Long.class);
//        final long[] predShape = type_pred.shape();

//        if (pred.numDimensions() != 1 || predShape[0] != 1) {
//            throw new RuntimeException(
//                    String.format(
//                            "Expected model to produce a [1] shaped tensor, instead it produced one with shape %s",
//                            Arrays.toString(predShape)));
//        }
//        long[] type_res = type_pred.copyTo(new long[1]);
        long[] action_res = action_pred.copyTo(new long[1]);
        long[] target_res = target_pred.copyTo(new long[1]);
//        System.out.println(classMap[(int)res[0]]);
        //Tensor<Float> prob = run.get(1).expect(Float.class);
        //final long[] rshape2 = prob.shape();

//        if (prob.numDimensions() != 2 || rshape2[0] != 1|| rshape2[1] != 6) {
//            throw new RuntimeException(
//                    String.format(
//                            "Expected model to produce a [1,6] shaped tensor, instead it produced one with shape %s",
//                            Arrays.toString(rshape2)));
//        }

        //float[][] res2 = prob.copyTo(new float[1][(int) rshape2[1]]);
//        System.out.println(res2[0][(int)res[0]]);

//        int i=0;
//        for(float re:res2[0]){
//            ClassifierResult resul;
//            resul = new ClassifierResult();
//            resul.setAnswer(classMap[i]);
//            resul.setScore(re);
//            results.add(resul);
//            i++;
//
//        }
//        type_pred.close();
        target_pred.close();
        action_pred.close();
        for(Tensor<?> r:run){
            r.close();
        }
//        ClassifierResult result = new ClassifierResult();
//        result.setAnswer(classMap[(int)res[0]]);
//        result.setScore(res2[0][(int)res[0]]);
        //Map<String,Float> resultMap = new HashMap<>();
        for(int idx=0;idx<action_res.length;idx++){
            results.add(new ClassifierResult(action_res[idx],target_res[idx]));
//            results.add(new ClassifierResult(type_res[idx],action_res[idx],target_res[idx]));
            //resultMap.put(classMap[idx],res2[0][idx]);
        }
        return results;
        //return resultMap;
    }
}
